﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Bus_proj.website
{
    public partial class round : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=MONSTERTUA;Initial Catalog=Bus_pro;User ID=sa1;Password=qwerty123");
        DataClasses1DataContext db = new DataClasses1DataContext();
        int z,a;
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {
                        BindingGrid();
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }
        }

        private void BindingGrid()
        {
            DataSet ds = new DataSet();
            conn.Open();
            string cmdstr = "Select * from Table_Route";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            cmd.ExecuteNonQuery();
            conn.Close();
            gvEmployeeDetails.DataSource = ds;
            gvEmployeeDetails.DataBind();
        }

        protected void gvEmployeeDetails_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblEmpID = (Label)e.Row.FindControl("lblEmpID");
                GridView gv_Child = (GridView)e.Row.FindControl("gv_Child");
                string txtempid = lblEmpID.Text;
                DataSet ds = new DataSet();
                conn.Open();
                string cmdstr = "Select * from Table_Point where Route_id=@Route_id";
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.Parameters.AddWithValue("@Route_id", txtempid);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                adp.Fill(ds);
                cmd.ExecuteNonQuery();
                conn.Close();
                gv_Child.DataSource = ds;
                gv_Child.DataBind();
            }
        }

        protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdY")
            {
                ModalPopupExtender1.Show();
                Panel1.Visible = true;
                Session["Route_id"] = Convert.ToInt32(e.CommandArgument.ToString());
                //Panel2.Visible = false;
            }
            if (e.CommandName == "cmdN")
            {
                a = Convert.ToInt32(e.CommandArgument.ToString());
                var q = from p in db.Table_Points
                        where p.Point_id == a
                        select p;
                try
                {
                    db.Table_Points.DeleteAllOnSubmit(q);
                    db.SubmitChanges();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ลบจุดจอดเสร็จสิ้น');", true);
                    //Response.Write(@"<script language='javascript'>alert('ลบจุดจอดเสร็จสิ้น');</script>");
                    BindingGrid();
                }
                catch (Exception)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถลบจุดจอดได้');", true);
                    //Response.Write(@"<script language='javascript'>alert('ลบจุดจอดเสร็จสิ้น');</script>");
                    //throw;
                    BindingGrid();
                }              
            }   
        }      

        protected void Button2_Click1(object sender, EventArgs e)
        {
            try
            {
                if (TextBox3.Text != "" && TextBox4.Text != "" && TextBox5.Text != "" && TextBox6.Text != "")
                {
                    Panel1.Visible = false;
                    Select SData = new Select();
                    z = SData.GetId_point();
                    Table_Point x = new Table_Point();
                    x.Point_id = z;
                    x.Route_id = Int32.Parse(Session["Route_id"].ToString());
                    x.Point_name = TextBox3.Text;
                    x.Point_charges = TextBox4.Text;
                    x.Point_distance = TextBox5.Text;
                    x.Point_tax = TextBox6.Text;
                    db.Table_Points.InsertOnSubmit(x);
                    db.SubmitChanges();
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มจุดจอดแล้ว');", true);
                    Panel2.Visible = true;
                    BindingGrid();
                }
                else
                {
                    Panel1.Visible = false;
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มจุดจอดได้');", true);
                    BindingGrid();
                }                
                //Response.Write(@"<script language='javascript'>alert('เพิ่มจุดจอดแล้ว');</script>");
                
            }
            catch (Exception)
            {
                Panel1.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มจุดจอดได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มจุดจอดได้');</script>");
                //throw;
                BindingGrid();
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                Table_Route x = new Table_Route();
                x.Route_id = Int32.Parse(TextBox1.Text);
                x.Route_source = DropDownList1.SelectedItem.ToString();
                x.Route_destination = DropDownList2.SelectedItem.ToString();
                x.Route_distance = TextBox2.Text;
                x.platform = TextBox9.Text;
                db.Table_Routes.InsertOnSubmit(x);
                db.SubmitChanges();
                Select SData = new Select();

                z = SData.GetId_point();
                Table_Point x_p = new Table_Point();
                x_p.Point_id = z;
                x_p.Route_id = Int32.Parse(TextBox1.Text);
                x_p.Point_name = DropDownList2.SelectedItem.ToString();
                x_p.Point_charges = TextBox7.Text;
                x_p.Point_distance = TextBox2.Text;
                x_p.Point_tax = TextBox8.Text;
                db.Table_Points.InsertOnSubmit(x_p);
                db.SubmitChanges();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('เพิ่มสายการเดินทางเสร็จสิ้น');", true);
                BindingGrid();                
                //Response.Write(@"<script language='javascript'>alert('เพิ่มสายการเดินทางเสร็จสิ้น');</script>");
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถเพิ่มสายการเดินทางได้');", true);
                BindingGrid();                
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถเพิ่มสายการเดินทางได้');</script>");               
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //Panel2.Visible = true;
            //Panel1.Visible = false;
        }  
       
    }
}